﻿using JTTD.Domain.Dtos;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace JTTD.Extensions
{
    /// <summary>
    /// dto数据模型验证
    /// </summary>
    public class DtoValidFilte : IActionFilter
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        public void OnActionExecuted(ActionExecutedContext context)
        {
            
        }

        /// <summary>
        /// dto数据模型验证
        /// </summary>
        /// <param name="context"></param>
        public void OnActionExecuting(ActionExecutingContext context)
        {
            if (!context.ModelState.IsValid)
            {
                var errorList= context.ModelState.SelectMany(item=>item.Value.Errors.Select(args=>args.ErrorMessage)).ToList();
                context.Result = new BadRequestObjectResult(new ApiResult<bool> { IsSuccess=false,Message= errorList.FirstOrDefault() });
            }
        }
    }
}
